/**
 * Author: Barry Wei
 * Data:   2008-08-15 
 */

var Grid = new Object();
Grid.Resource = {
	nullSrc: "src is Null"
};

Grid.create = function(config){
	this.src;
	this.pageNo;
	this.maxNum;
	this.ajaxType;
	this.sortHtmlAsc;
	this.sortHtmlDesc;
	this.sortColumnName;
	this.isAscSort;
	this.firstSort = true;
	this.init(config);
}

Grid.create.prototype = {
	init:function(config){
		if (!config.src){
			alert(Grid.Resource.nullSrc);
			return false;
		}
		
		this.pageNo = config.pageNo ? config.pageNo : 1;
		this.maxNum = config.maxNum ? config.maxNum : 10;
		if (config.src.indexOf("?") != -1){
			this.src = config.src + "&pageNo=" + this.pageNo + "&maxNum=" + this.maxNum;
		}else{
			this.src = config.src + "?pageNo=" + this.pageNo + "&maxNum=" + this.maxNum;
		}
		this.sortHtmlAsc = config.sortHtmlAsc ? config.sortHtmlAsc : "\u2191";
		this.sortHtmlDesc = config.sortHtmlDesc ? config.sortHtmlDesc : "\u2193";
	},
	getXml:function(params, callBack){
		if (this.src.length > 2048){
			this.ajaxType = "POST";
		}else{
			this.ajaxType = "GET";
		}			
		$.get(this.src, params, callBack);
	},
	parseXml:function(xml){
		var columnsArray = Grid.create.getGridColumns();
		
		var xmlDom;
		if (window.ActiveXObject){
			xmlDom = new ActiveXObject("Microsoft.XMLDOM");
			xmlDom.loadXML(xml);
		}else{
			var parser = new DOMParser();
			xmlDom = parser.parseFromString(xml, "text/xml");
		}		
		
		var rows = $("grid > row", xmlDom);
		
		var dataTable = $("table.gridTable");
		var dataRow = $("tr[@class!=headRow]", dataTable);
		dataRow.remove();
		
		for (var i=0; i < rows.size(); i++)
		{
			if(i % 2 == 0)
			{
				dataTable.append("<tr class='dataRow1'></tr>");
			}else
			{
				dataTable.append("<tr class='dataRow2'></tr>");
			}
			
			for (var j=0; j < columnsArray.length; j++){
				var temp = $(columnsArray[j], rows.eq(i)).text();
				var columnTd = $("tr[@class=headRow] > td", dataTable).eq(j)
				var displayOption = columnTd.css("display");
				var nowrapOption = columnTd.attr("nowrap");
				if(nowrapOption == true){
					nowrapOption = " nowrap";
				}else{
					nowrapOption = "";
				}
				$("tr:last", dataTable).append("<td" + nowrapOption + " style='display:" + displayOption + "'>" + temp + "</td>");
			}
		}

		$("tr[@class!=headRow]", dataTable).bind("mouseover", function(){
			$(this).children().attr("class", "currDataRow");
		});
		
		$("tr[@class!=headRow]", dataTable).bind("mouseout", function(){
			$(this).children().removeAttr("class");
		});
		
		// when the submit action is destroy, hidden the loadDiv
		$("div#loadDiv").css("display", "none");		
	},
	sortColumn:function(property, sortType){
		if(sortType){
			sortType = sortType.toLowerCase();
		}else{
			sortType = "string";
		}
		
		var gridTable = $("table.gridTable");
		var index = $("tr[@class=headRow] > td", gridTable).index($("tr td[@property=" + property + "]"), gridTable);
		var dataRows = $("tr[@class!=headRow]", gridTable);
		
		var sortTd = $("tr[@class=headRow] > td[@property=" + property + "]", gridTable);
		
		var sortHtmlAsc = this.sortHtmlAsc;
		var sortHtmlDesc = this.sortHtmlDesc;
		if(!this.firstSort){
			$("tr[@class=headRow] > td", gridTable).each(function(){
				var ascIndex = $(this).html().indexOf(sortHtmlAsc);
				var descIndex = $(this).html().indexOf(sortHtmlDesc);
				if( ascIndex >= 0){
					$(this).html($(this).html().substr(0, ascIndex) + $(this).html().substr(ascIndex + sortHtmlAsc.length));
				}
				else if(descIndex >= 0){
					$(this).html($(this).html().substr(0, descIndex) + $(this).html().substr(descIndex + sortHtmlDesc.length));
				}
			});
			
		}
		
		if(this.sortColumnName == property && this.isAscSort){
			Grid.create.sortDesc(dataRows, index, sortType);
			sortTd.html(this.sortHtmlDesc + sortTd.html());
			this.isAscSort = false;
			this.sortColumnName = property;
			this.firstSort = false;
		}else{
			Grid.create.sortAsc(dataRows, index, sortType);
			sortTd.html(this.sortHtmlAsc + sortTd.html());
			this.isAscSort = true;
			this.sortColumnName = property;
			this.firstSort = false;
		}
		
	}
};

// Get grid Columns
Grid.create.getGridColumns = function(){
	var columnsArray = new Array;
	var gridTable = $("table.gridTable");
	var columns = $( "tr.headRow > td", gridTable);
	columns.each(function(){
		columnsArray.push($(this).attr("property"));
	});
	return columnsArray;		
}

Grid.create.sortAsc = function(dataRows, index, sortType){
	var dataArray = new Array;
	for(var ii = 0; ii < dataRows.length; ii ++){
		dataArray.push(dataRows.eq(ii).children().eq(index).html());
	}
	
	for (var i = dataRows.length-1; i >= 0 ; --i){
		for ( var j = 1; j <= i; ++j){
			if ((sortType == "number" && parseFloat(dataArray[j]) < parseFloat(dataArray[j-1]))
				|| (sortType != "number" && (dataArray[j].localeCompare(dataArray[j-1]) < 0))){
				var temp = dataRows.eq(j).html();
				dataRows.eq(j).html( dataRows.eq(j-1).html() );
				dataRows.eq(j-1).html( temp );
				
				var tempNode = dataArray[j];
				dataArray[j] = dataArray[j-1];
				dataArray[j-1] = tempNode;
			}
		}
	}
}

Grid.create.sortDesc = function(dataRows, index, sortType){
	var dataArray = new Array;
	for(var ii = 0; ii < dataRows.length; ii ++){
		dataArray.push(dataRows.eq(ii).children().eq(index).html());
	}
	
	for (var i = dataRows.length-1; i >= 0 ; --i){
		for ( var j = 1; j <= i; ++j){
			if ((sortType == "number" && parseFloat(dataArray[j]) > parseFloat(dataArray[j-1]))
				|| (sortType != "number" && (dataArray[j].localeCompare(dataArray[j-1]) > 0))){
				var temp = dataRows.eq(j).html();
				dataRows.eq(j).html( dataRows.eq(j-1).html() );
				dataRows.eq(j-1).html( temp );
				
				var tempNode = dataArray[j];
				dataArray[j] = dataArray[j-1];
				dataArray[j-1] = tempNode;
			}
		}
	}
}

$(document).ready(function(){
	// When the page load over, hidden the loadDiv
	$("div#loadDiv").css("display", "none");
	
	var gridTable = $("table.gridTable");
	
	$("form").bind("submit", function(){
		// when submit, show the loadDiv
		$("div#loadDiv").css("display", "block");
		var formActionSrc = $(this).attr("action");
		var pageNo = $("input#pageNo").val();
		var grid = new Grid.create({
			src:    formActionSrc,
			pageNo: pageNo,
			maxNum: 10,
			sortHtmlAsc: "\u2191",
			sortHtmlDesc: "\u2193"
		});
		
		grid.getXml(null, grid.parseXml);
		$("tr[@class='headRow'] > td", gridTable).unbind("click");
		$("tr[@class='headRow'] > td", gridTable).bind("click", function(){
			grid.sortColumn($(this).attr("property"), $(this).attr("dataType"));
		});
		
		// prevent submit
		return false;
	});
});
